package cn.iocoder.yudao.module.water.controller.admin.qtoriginal;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.water.controller.admin.qtoriginal.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.qtoriginal.QtOriginalDO;
import cn.iocoder.yudao.module.water.convert.qtoriginal.QtOriginalConvert;
import cn.iocoder.yudao.module.water.service.qtoriginal.QtOriginalService;

@Api(tags = "管理后台 - 原水记录")
@RestController
@RequestMapping("/water/qt-original")
@Validated
public class QtOriginalController {

    @Resource
    private QtOriginalService qtOriginalService;

    @PostMapping("/create")
    @ApiOperation("创建原水记录")
    @PreAuthorize("@ss.hasPermission('water:qt-original:create')")
    public CommonResult<Long> createQtOriginal(@Valid @RequestBody QtOriginalCreateReqVO createReqVO) {
        return success(qtOriginalService.createQtOriginal(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新原水记录")
    @PreAuthorize("@ss.hasPermission('water:qt-original:update')")
    public CommonResult<Boolean> updateQtOriginal(@Valid @RequestBody QtOriginalUpdateReqVO updateReqVO) {
        qtOriginalService.updateQtOriginal(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除原水记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:qt-original:delete')")
    public CommonResult<Boolean> deleteQtOriginal(@RequestParam("id") Long id) {
        qtOriginalService.deleteQtOriginal(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得原水记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:qt-original:query')")
    public CommonResult<QtOriginalRespVO> getQtOriginal(@RequestParam("id") Long id) {
        QtOriginalDO qtOriginal = qtOriginalService.getQtOriginal(id);
        return success(QtOriginalConvert.INSTANCE.convert(qtOriginal));
    }

    @GetMapping("/list")
    @ApiOperation("获得原水记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('water:qt-original:query')")
    public CommonResult<List<QtOriginalRespVO>> getQtOriginalList(@RequestParam("ids") Collection<Long> ids) {
        List<QtOriginalDO> list = qtOriginalService.getQtOriginalList(ids);
        return success(QtOriginalConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得原水记录分页")
    @PreAuthorize("@ss.hasPermission('water:qt-original:query')")
    public CommonResult<PageResult<QtOriginalRespVO>> getQtOriginalPage(@Valid QtOriginalPageReqVO pageVO) {
        PageResult<QtOriginalDO> pageResult = qtOriginalService.getQtOriginalPage(pageVO);
        return success(QtOriginalConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出原水记录 Excel")
    @PreAuthorize("@ss.hasPermission('water:qt-original:export')")
    @OperateLog(type = EXPORT)
    public void exportQtOriginalExcel(@Valid QtOriginalExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<QtOriginalDO> list = qtOriginalService.getQtOriginalList(exportReqVO);
        // 导出 Excel
        List<QtOriginalExcelVO> datas = QtOriginalConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "原水记录.xls", "数据", QtOriginalExcelVO.class, datas);
    }

}
